﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Data.Sql;

public partial class Unset : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string Ono = Request.QueryString["Ono"];
            string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStrCustomer"].ConnectionString;
            List<string> OnoList = new List<string>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                //先通过Aaccount找到所有订单号Dno日期作为约束条件
                //再根据Dno，找到所有信息。
                try
                {
                    conn.Open();
                    string sqlString = "select * from Customer_ViewOrderHist where Ono = @Ono";
                    SqlParameter[] tempParameters = { 
                                                    new SqlParameter("@Ono", Ono)
                                                };
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sqlString;
                        cmd.Parameters.AddRange(tempParameters);
                        SqlDataReader dr = cmd.ExecuteReader();
                        if (dr != null)
                        {
                            double sum = 0;
                            string Osumprice;
                            string AvipLel;
                            dr.Read();

                            //填写订单信息
                            fillOrderInf(dr, Table1);
                            Osumprice = dr["Osumprice"].ToString();
                            AvipLel = dr["AvipLel"].ToString();
                            //填写菜单信息
                            do
                            {
                                sum += fillDishInf(dr, Table1);
                            } while (dr.Read());
                            //填写总计
                            TableRow row = new TableRow();
                            row = new TableRow();
                            AddCell(row, "原价");
                            AddCell(row, sum.ToString() + "￥");
                            AddCell(row, "会员折后价");
                            AddCell(row, Osumprice);
                            AddCell(row, "VIP等级：");
                            AddCell(row, AvipLel);
                            Table1.Rows.Add(row);
                        }
                        dr.Close();
                    }
                }
                catch (Exception ez)
                {
                    Response.Write(@"<b>异常信息:</b>" + ez.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
        }
    }
    private void AddCell(TableRow row, string str)
    {
        TableCell cell = new TableCell();
        Label lab = new Label();
        lab.Text = str;
        cell.Width = new Unit(100);
        cell.Controls.Add(lab);
        row.Cells.Add(cell);
    }
    private void fillOrderInf(SqlDataReader dr, Table OrderListTable)
    {
        TableRow row = new TableRow();
        //第一行
        row = new TableRow();
        //订单号
        AddCell(row, "订单号：");
        AddCell(row, dr["Ono"].ToString());
        //下单时间
        AddCell(row, "下单时间：");
        AddCell(row, dr["SOtime"].ToString());

        OrderListTable.Rows.Add(row);

        //第二行
        row = new TableRow();
        //收餐人姓名
        AddCell(row, "收餐人姓名：");
        AddCell(row, dr["Oname"].ToString());
        //收餐联系方式
        AddCell(row, "收餐联系方式：");
        AddCell(row, dr["Ophone"].ToString());

        OrderListTable.Rows.Add(row);

        //第三行
        row = new TableRow();
        //收餐人地址
        AddCell(row, "收餐人地址：");
        AddCell(row, dr["Oadr"].ToString());
        //收餐联系方式
        AddCell(row, "订餐留言：");
        AddCell(row, dr["SOremarks"].ToString());
        OrderListTable.Rows.Add(row);

        //第四行
        AddCell(row, "订单状态");
        string state = dr["Ostate"].ToString();

        AddCell(row, state);
        
        OrderListTable.Rows.Add(row);
    }
    private double fillDishInf(SqlDataReader dr, Table OrderListTable)
    {
        TableRow row = new TableRow();
        double sum = 0;
        row = new TableRow();
        AddCell(row, dr["Dname"].ToString());
        AddCell(row, dr["Dprice"].ToString() + "￥");
        AddCell(row, "×" + dr["Cnum"].ToString());
        sum = double.Parse(dr["Dprice"].ToString()) * int.Parse(dr["Cnum"].ToString());
        AddCell(row, "=  " + sum.ToString() + "￥");
        OrderListTable.Rows.Add(row);
        return sum;
    }
    protected void UnsetBut_Click(object sender, EventArgs e)
    {
        string Ono = Request.QueryString["Ono"];
        HttpCookie usr = Request.Cookies["usr"];
        string Aaccount = usr.Value;
        string USreason = USreasonTex.Text;

        //更新Unset表
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStrCustomer"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            try
            {
                conn.Open();
                SqlTransaction trans = conn.BeginTransaction();

                string sqlString = "insert into Customer_Unset (Ono , Aaccount , UStime , USreason,USstate) values(@Ono , @Aaccount ,  getdate(), @USreason ,@USstate)";
                string USstate = "申请退单";
                SqlParameter[] parameters = { 
                                                        new SqlParameter("@Ono", Ono),
                                                        new SqlParameter("@Aaccount", Aaccount),
                                                        new SqlParameter("@USreason", USreason),
                                                        new SqlParameter("@USstate", USstate),
                                                    };
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.Transaction = trans;
                    cmd.CommandText = sqlString;
                    cmd.Parameters.AddRange(parameters);
                    int ret = cmd.ExecuteNonQuery();
                    if (ret > 0)
                    {
                    }
                    else
                    {
                        Response.Write(@"<script>alert('下订失败');</script>");
                    }
                }

                sqlString = "update Customer_Order set Ostate = @Ostate where Ono = @Ono ";
                string Ostate = "申请退单";
                SqlParameter[] parametersNew = { 
                                                new SqlParameter("@Ono", Ono),
                                                new SqlParameter("@Ostate", Ostate)
                                            };
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.Transaction = trans;
                    cmd.CommandText = sqlString;
                    cmd.Parameters.AddRange(parametersNew);
                    int ret = cmd.ExecuteNonQuery();
                    if (ret > 0)
                    {
                    }
                    else
                    {
                        Response.Write(@"<script>alert('下订失败');</script>");
                        return;
                    }
                }
                trans.Commit();
                return;
            }
            catch (Exception ez)
            {
                Response.Write(@"<b>异常信息:</b>" + ez.Message);
            }
            finally
            {
                conn.Close();
            }
            Response.Redirect("./CheckOrder.aspx");
        }
    }
}